package com.javaymw.demo.service.impl;

import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import com.alibaba.fastjson.JSON;
import com.javaymw.demo.base.AccessToken;
import com.javaymw.demo.base.Constants;
import com.javaymw.demo.base.Result;
import com.javaymw.demo.service.QiYeWxApiService;

@Service
public class QiYeWxApiServiceImpl implements QiYeWxApiService {

	private static Logger LOGGER = LoggerFactory.getLogger(QiYeWxApiService.class);
	
	@Autowired
	private RestTemplate restTemplate;
	
	@Override
	public AccessToken getAccessToken() {
		AccessToken accessToken = null;
		Map<String,String> params = new HashMap<>(2);
        params.put("ID", Constants.CORPID);
        params.put("SECRET", Constants.CORPSECRET);
        //restTemplate GET 请求
        accessToken = restTemplate.getForObject(Constants.ACCESS_TOKER_URL, AccessToken.class, params);
        LOGGER.info("accessToken: {}", JSON.toJSON(accessToken));
		return accessToken;
	}

	@Override
	public Result oAuth2GetUserByCode(String token, String code, String agentId) {
		Result result = new Result();
//		String menuUrl = WeiXinQiYeConstants.GET_OAUTH2_URL.replace("ACCESS_TOKEN", token).replace("CODE", code)
//				.replace("AGENTID", agentId + "");
//		String userinfo = JHttpUtils.doGet(menuUrl);
//		logger.info("userinfo: " + userinfo);
//		JSONObject jsonObject = null;
//		if (userinfo != null) {
//			try {
//				jsonObject = JSONObject.fromObject(userinfo);
//				logger.info("jsonObject: " + jsonObject);
//				if (jsonObject.getString("UserId") != null && jsonObject.getString("UserId").length() > 0) {
//					result.setErrmsg(jsonObject.getString("errmsg"));
//					result.setErrcode(jsonObject.getString("errcode"));
//					result.setUserId(jsonObject.getString("UserId"));
//				} else {
//					result.setErrmsg(jsonObject.getString("errmsg"));
//					result.setErrcode(jsonObject.getString("errcode"));
//				}
//			} catch (Exception e) {
//				result.setErrmsg("accessToken 超时......");
//				result.setErrcode("42001");
//			}
//		}
		return result;
	}

}
